// 订单交易也需要存储交易信息和地址，加在仓库方便管理
import {reqGetTradeData,reqGetUserAddressList} from '@/api'
const state = {
  // 交易信息，对象
  tradeInfo:{},
  // 收获地址是数组
  userAddressList:[]
}

const actions = {
  //  获取订单中的商品数据
  async getTradeListData({commit}){
    const result = await reqGetTradeData()
    if(result.code === 200) {
      console.log('获取订单中的商品数据',result.data);
      commit('SAVE_TRADE_LIST_DATA',result.data)
    } else {
      console.log(result.message);
    }
  },

  // 获取用户的收获地址
  async getUserAddressList({commit}){
    const result = await reqGetUserAddressList
    if(result.code === 200) {
      console.log('获取用户的收获地址',result.data);
      commit('SAVE_USER_ADDRESS_LIST',result.data)
    } else {
      console.log(result.message);
    }
  }
}

const mutations = {
  // 存储商品信息
  SAVE_TRADE_LIST_DATA(state,payload){
    state.tradeInfo = payload
  },

  // 存储用户地址列表
  SAVE_USER_ADDRESS_LIST(state,addressList){
    state.userAddressList = addressList
  }
}

const getters = {
  // 要渲染的数据
  // 商品列表
  detailArrayList(state){
    return state.tradeInfo.detailArrayList
  },

  // originalTotalAmount totalAmount totalNum tradeNo
  // 商品总金额
  originalTotalAmount(state){
    return state.tradeInfo.originalTotalAmount
  },
 // 实付金额
  totalAmount(state){
    return state.tradeInfo.totalAmount
  },
// 商品数量
  totalNum(state){
    return state.tradeInfo.totalNum
  },
// 订单编号
  tradeNo(state){
    return state.tradeInfo.tradeNo
  },
  // 返现金额
  activityReduceAmount(state){
    return state.tradeInfo.activityReduceAmount
  }
}


export default {
  namespaced:true,
  state,
  actions,
  mutations,
  getters
}